//数据
//数据
var data = [{
    name: 'Jane',
    data: [["一",50.5], ["二",40.8], ["三",64.5],["四",100],["五",10],["六",88],["七",75]]
}];

var data1 = ["一","二"];
var data2 = [1, 2,3,4,1,2,5,2];


var myChart = echarts.init(document.getElementById('container'));
var option = {
    title : {
        text: '',
        subtext: '人均学时(小时)'
    },
    tooltip : {
        trigger: 'axis'
    },
    color:['#ffac32','#2db7fb','#4df0f0','#f3e24f'],
    toolbox: {
        show : true,
    },
    legend: {
        data:['人均学时(小时)']
    },
    calculable : true,
    grid:{
        y2:140
    },
    xAxis : [
        {
            type : 'category',
            width : '10%',
            axisLabel:{
                interval:0,
                rotate:40
            },
            data : data1
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
    ]
};


//图片对象
var chartObj;


// 隐藏所有内容区

var hideContent = function(){
    $(".content").each(function(index){
        $(this).hide("slow");
    });
}
var bindTitle = function(){
    $(".title").each(function(){
        $(this).click(function(){
            hideContent();
            $(this).next().show("slow");
        });
    });
}

//报表
var highcharts_init = function(companyId){
    var params = {"companyId" : companyId};
    var data_url = appPath + '/supervise/highcharts';
    var dt = ajax(data_url, params);
    var result = dt['result'];
    data = JSON.parse(result);
    option.xAxis.categories = ['1', '2', '3'];

    option.series = data;

    drawImage();

    //隐藏设置区内容
    hideContent();
    //给标题绑定事件
    bindTitle();

    $("g.highcharts-button").remove();
};

var result_list;
var highcharts_data = function(result, companyAry){
    result_list = result;
    var data = result_list[0].data;
    data1 = [];
    data2 = [];
    for(var i = 0;i< data.length; i++){
        data1.push(data[i][0]);
        data2.push(data[i][1]);
    }
    option.xAxis[0].data = data1;
    option.series = [];
    option.series.push(series(data2));
    //option.series.push(series(data2));

    drawImage();

    //隐藏设置区内容
    hideContent();
    //给标题绑定事件
    bindTitle();

    changeType();

    $("g.highcharts-button").remove();
    $("g.highcharts-legend").remove();
};

var ajax = function(url, param){
    var result = '';
    $.ajax({
        url: url,
        async: false,
        type: 'post',
        data: param,
        success: function(data){
            result = data;
        }
    });
    return result;
};

//绘制图表方法，每次绘制完成后，将绘制的图标存入变量chartObj
var drawImage = function(){
    myChart.setOption(option);
}

//更换图表类型，通过修改json对象option的值来修改图表，修改之后绘制图表
var changeType = function(){
    var valueName = $("input[name=valueName]:checked").val();
    // option.chart.type = typeName;
    if( valueName == 0 ){
        var colors = ['#509bfd','#58befd'];
        option.color = colors;
    }else if(valueName == 1){
        var colors = ['#4bd128','#70e351'];
        option.color = colors;

    }else if( valueName == 2 ){
        var colors = ['#fed547','#fee079'];
        option.color = colors;
    }
    option.series[0] = result_list[valueName];
    option.title.subtext = $("input[name=valueName]:checked").attr("title");

    drawImage();
    $("g.highcharts-button").remove();
    $("g.highcharts-legend").remove();
}

//更换图表的标题，可以通过修改json对象option来修改，也可以通过highcharts自带的方法修改

var changeTitle = function(){
    var titleTemp = $("#changeTitle").val();
    var subtitleTemp = $("#changeSubTitle").val();
    if(titleTemp != "" && titleTemp != null && subtitleTemp != "" && subtitleTemp != null){
        chartObj.setTitle({
            text:titleTemp
        },{
            text:subtitleTemp
        });
    }else {
        if(titleTemp != "" && titleTemp != null){
            option.title.text = titleTemp;
        }else if(subtitleTemp != "" && subtitleTemp != null){
            option.subtitle.text = subtitleTemp;
        }
        drawImage();
    }
}

//数据的格式可以参考data对象，在数据中，可以设置各种数据的属性
var changeData = function(){
    option.series = data;
    drawImage();
}

//控制标签显隐
var legendFlag = true;
var showOrHideLegend = function(){
    if(!option.legend){
        option.legend = {};
    }
    if(legendFlag){
        option.legend.enabled = false;
        legendFlag = false;
        $("#legend").html("显示标签");
    }else{
        option.legend.enabled = true;
        legendFlag = true;
        $("#legend").html("隐藏标签");
    }
    drawImage();
}

// 改变标签的位置

var changeLegend = function(position){
    if(!option.legend){
        option.legend = {};
    }
    option.legend.align = position;
    drawImage();
}


// 控制版本信息显隐

var creditFlag = false;
var showOrHideCredits = function(){
    if(!option.credits){
        option.credits = {}
    }
    if (creditFlag) {
        option.credits.enabled = false;
        creditFlag = false;
        $("#credits").html("显示版本信息");
    }else{
        option.credits.enabled = true;
        creditFlag = true;
        $("#credits").html("隐藏版本信息");
    }
    drawImage();
}

//	控制版本设置

var changeCredits = function(){
    if(!option.credits){
        option.credits = {}
    }
    var creditsText = $("#creditsText").val();
    var creditsHref = $("#creditsHref").val();
    if(creditsText != "" && creditsText != null){
        option.credits.text = creditsText;
    }else if(creditsHref != "" && creditsHref != null){
        if (creditsHref.indexOf("www")>=0) {
            creditsHrefTemp = "http://" +　creditsHref;
        }
        option.credits.href = creditsHrefTemp;
    }
    drawImage();
}

//	修改X轴坐标值

var changeCategories = function(){
    var categories = $("#categories").val();
    // console.log(categories);
    var categoriesArray = categories.split(" ");
    // console.log(categoriesArray);
    option.xAxis.categories = categoriesArray;
    drawImage();

}

function series(data) {
    return {
        name:'',
        type:'bar',
        data:data,
        itemStyle: {

            normal: {
                barBorderRadius :[8,8,0,0]  ,
                label: {
                    show : false,
                }
            }
        },
        barWidth:30,
        markPoint : {
            data : [
                {type : 'max', name: '最大值'},
                {type : 'min', name: '最小值'}
            ]
        },
        markLine : {
            data : [
                {type : 'average', name: '平均值'}
            ]
        }
    }
}